// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  traceUser: true,
  env:'aaa111-9gh9fp5ieac4de5d'
})

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    var dayId = event.dayId
    var dayIndex = event.dayIndex
    var tripId = event.tripId

    await db.collection('trip').where({
        _id: tripId
    }).get().then(res => {
        console.log(res.data[0])
        //
        var days = res.data[0].days
        var tripEndTime = res.data[0].tripEndTime
        console.log(days, tripEndTime)

        days.splice(dayIndex,1)
        tripEndTime = tripEndTime - (1 * 24 * 60 * 60 * 1000)
        console.log(days, tripEndTime)
        //
        db.collection('trip').where({
            _id: tripId
        }).update({
            data: {
                tripEndTime: tripEndTime,
                days: days
            }
        }).catch(err => {
            console.error(err)
        })
        //
        db.collection('tripDays').where({
            _id: dayId
        }).remove().catch(err => {
            console.error(err)
        })
    }).catch(err => {
        console.error(err)
    })

    return true
}